import sun.reflect.generics.tree.Tree;

import java.util.*;
class Node {
    int val;
    Node next;
    Node left;
    Node right;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}

public class TestDemo {
    public static Map<Integer,Integer> func1(int[] array) {   //统计一个数组中每个数出现的次数
        Map<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < array.length; i++) {
            int ret = map.getOrDefault(array[i],0);  //查看key对应的value，没有key则返回0；
            if(ret == 0) {                      //如果为0，则代表没有，添加进去
                map.put(array[i],1);
            }
            if(ret > 0) {                       //不等于0，说明key存在，则设置value+1；
                //int value = map.get(array[i]);
                map.put(array[i],ret+1);
            }
        }
        return map;
    }
    public static Set<Integer> func2(int[] array) {    //去重复数值
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < array.length; i++) {
            set.add(array[i]);
        }
        return set;
    }
    public static int func3(int[] array) {                   //找到第一个出现重复的值
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < array.length; i++) {
            if(set.contains(array[i])) {
                return array[i];
            }
            set.add(array[i]);
        }
        return -1;
    }
    public static int func4(int[] array) {   //找到只出现一次的值
        Set<Integer> set = new HashSet<>();
        set.add(array[0]);
        for (int i = 1; i < array.length; i++) {
            if(set.contains(array[i])) {
                set.remove(array[i]);
            } else {
                set.add(array[i]);
            }
            //set.add(array[i]);
        }
        for (int i = 0; i < array.length; i++) {
            if(set.contains(array[i])) {
                return array[i];
            }else {
                continue;
            }
        }
        return -1;
    }
    public Node copyRandomList(Node head) {       //复制随机链表
        Map<Node,Node> map = new HashMap<>();
        Node cur = head;
        while (cur != null) {
            map.put(cur,new Node(cur.val));
            cur = cur.next;
        }
        cur = head;
        while (cur != null) {
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }
        return map.get(head);
    }
    public int numJewelsInStones(String jewels, String stones) {
        int count = 0;
        Set<String> set = new HashSet<>();
        for (int i = 0; i < jewels.length(); i++) {
            set.add(jewels.charAt(i)+"");
        }
        for (int j = 0; j < stones.length(); j++) {
            if(set.contains(stones.charAt(j)+"")) {
                count++;
            }
        }
        return count;
    }
    public static void func(String strExce,String strActual){
        Set<Character> set = new HashSet<>();
        Set<Character> set1 = new HashSet<>();
        for (int i = 0; i < strActual.length(); i++) {
            set.add(strActual.toUpperCase().charAt(i));
        }
        for (int j = 0; j < strExce.length(); j++) {
            if(set.contains(strExce.toUpperCase().charAt(j))){
               continue;
            }else {
                System.out.println(strExce.toUpperCase().charAt(j));
                set1.add(strExce.toUpperCase().charAt(j));
            }
        }
    }

    public List<String> topKFrequent(String[] words, int k) {
        Map<String,Integer> map = new HashMap<>();
        //统计每个单词出现次数
        for (String word :words) {
            map.put(word,map.getOrDefault(word,0)+1);
        }
        //建立小根堆
        PriorityQueue<String> minHeap = new PriorityQueue<>(k, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                if(map.get(o1).equals(map.get(o2))) {
                    return o2.compareTo(o1);
                }
                return map.get(o1)-map.get(o2);
            }
        });
        //依次向堆加入
        for (String word:map.keySet()) {
            minHeap.offer(word);
            if(minHeap.size()>k) {
                minHeap.poll();
            }
        }
        List<String> list = new ArrayList<>();
        while (minHeap.size()>0) {
            list.add(minHeap.poll());
        }
        Collections.reverse(list);
        return list;
    }

    public static Node find(Node root,int key) {
        Node cur = root;
        while (cur != null) {
            if(cur.val > key) {
                cur = cur.left;
            } else if(cur.val < key) {
                cur = cur.right;
            }else {
                break;
            }
        }
        return cur;
    }
    public static boolean insert(Node root,int key){
        if(root == null) {
            root = new Node(key);
            return true;
        }
        Node parent = null;
        Node cur = root;
        while (cur != null) {
            if(cur.val > key) {
                parent = cur;
                cur = cur.left;
            } else if(cur.val < key) {
                parent = cur;
                cur = cur.right;
            }else {
                return false;
            }
        }
        Node node = new Node(key);
        if(key > parent.val) {
            parent.right = node;
        }else {
            parent.left = node;
        }
        return true;
    }
    public static int[] MySort (int[] arr) {
        // write code here
        int begin = 0;
        int end = arr.length-1;
        MergeSortHelp(arr,begin,end);
        return arr;
    }
    public static void MergeSortHelp(int[] array,int begin,int end) {
        if(begin >= end) return;
        int mid = begin+((end-begin)>>>1);
        MergeSortHelp(array,begin,mid);
        MergeSortHelp(array,mid+1,end);
        func(array,begin,end,mid);
    }
    public static void func(int[] array,int begin,int end ,int mid) {
        int s1 = 0;
        int s2 = mid+1;
        int e1 = mid;
        int e2 = end;
        int[] newarray = new int[end-begin+1];
        for(int i = 0;i <newarray.length;i++) {
            if(s1>e1) {
                newarray[i] = array[s2++];
                continue;
            }
            if(s2>e2) {
                newarray[i] = array[s1++];
                continue;
            }
            if(array[s1] > array[s2]) {
                newarray[i] = array[s2++];
            }else {
                newarray[i] = array[s1++];
            }
        }
        for(int i = 0;i<newarray.length;i++) {
            array[i+begin] = newarray[i];
        }
    }

    public static void main(String[] args) {
        int[] array = {1,2,3,6,54,8,7,9};
        int[] ret = MySort(array);
        System.out.println(Arrays.toString(ret));
    }
    public static void main2(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String str1 = scanner.nextLine();
            String str2 = scanner.nextLine();
            func(str1,str2);
        }
    }
    public static void main1(String[] args) {
        int[] array = {1,2,2,3,3,4,4,5,5,6,6};
        int[] array1 = {1,1,2,2,3,3,4,5,5,6,6};
        Map<Integer,Integer> map = func1(array);
        Set<Integer> map1 = func2(array);
        System.out.println(map);
        System.out.println(map1);
        System.out.println(func3(array));
        System.out.println(func4(array1));
    }
}
